package com.system.connect.springboot_flowers.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.connect.springboot_flowers.entity.Comments;
import com.system.connect.springboot_flowers.dto.CommentsDto;
import com.system.connect.springboot_flowers.entity.Originalposts;
import com.system.connect.springboot_flowers.entity.PostMetrics;
import com.system.connect.springboot_flowers.entity.Users;
import com.system.connect.springboot_flowers.service.*;
import com.system.connect.springboot_flowers.utils.R;
import com.system.connect.springboot_flowers.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author yang
 * @since 2025-01-13
 */
@RestController
@RequestMapping("/api/comments")
public class CommentsController {

    @Autowired
    private ICommentsService commentsService;

    @Autowired
    private IUsersService UserService;

    @Autowired
    private IOriginalpostsService PostsService;

    @Autowired
    private IPostMetricsService postMetricsService;

    @PostMapping("/postsave")
    public R save(Integer postsid, String content){
        Comments comments = new Comments();
        Originalposts post = PostsService.getById(postsid);
        //寻找发帖用户人
        //更新帖子时间
        Users user1 = UserService.getById(post.getUserId());//帖子作者
        Users user = UserService.getById(TokenUtils.getUser());//发布人

        if (content!=null){
            comments.setContent(content);
            comments.setPostId(postsid);
            comments.setPostId(postsid);
            comments.setUserId(Integer.valueOf(TokenUtils.getUser()));
            comments.setCategory("originalpost");
            comments.setContent(content);
            comments.setCreatedAt(LocalDateTime.now());
            commentsService.save(comments);

            //更新帖子评论量
            QueryWrapper<PostMetrics> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("id", "post_id", "views", "replies", "favorites") // 移除了 'likes'
                    .eq("post_id", postsid);
            PostMetrics postMetrics = postMetricsService.getOne(queryWrapper);
            if (postMetrics != null) {
                postMetrics.setReplies(postMetrics.getReplies() + 1);
                postMetricsService.updateById(postMetrics);
            }

            return R.success("评论成功");
        }
        return R.error("评论失败");
    }

    /**
     * 获取帖子评论
     */
    @GetMapping("/{post_id}") // 确保路径与前端请求匹配
    public R list(@PathVariable("post_id") Integer postId) {
        Page<Comments> pageInfo = new Page<>();
        QueryWrapper<Comments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("post_id",postId);

        ArrayList<Comments> comments = (ArrayList<Comments>) commentsService.list(queryWrapper);
        ArrayList<CommentsDto> commentsDtos = new ArrayList<>();
        for (Comments comment : comments) {
            CommentsDto commentsDto = new CommentsDto();
            Users user = UserService.getById(comment.getUserId());
            commentsDto.setCommentavatar(user.getAvatarUrl());
            commentsDto.setContent(comment.getContent());
            commentsDto.setState(comment.getState());
            commentsDto.setCreatedAt(comment.getCreatedAt());
            commentsDto.setUserId(comment.getUserId());
            commentsDto.setCommentavatar(user.getAvatarUrl());
            commentsDto.setPostId(comment.getPostId());
            commentsDtos.add(commentsDto);

        }

        return R.success(commentsDtos);
    }

}
